package com.house.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.house.pojo.Admin;
import com.house.pojo.Houseuser;
import com.house.service.AdminService;
import com.house.service.HouseuserService;

@Controller
@Scope("prototype")
public class LoginController {
	@Autowired
	private AdminService adminservice;
	@Autowired
	private HouseuserService HouseuserService;
	//鐧婚檰
	@RequestMapping("/adminlogin")
	public String login(HttpSession session, String adminId,String password) throws Exception{
		//璋冪敤service杩涜鐢ㄦ埛韬唤楠岃瘉
		Admin admin = adminservice.findAdminByIdandpassword(adminId, password);
		if(admin != null){
		session.removeAttribute("userId");
		//鍦╯ession涓繚瀛樼敤鎴疯韩浠戒俊鎭�
		session.setAttribute("adminname", admin.getAdminName());
		session.setAttribute("adminId", admin.getAdminId());
		//閲嶅畾鍚戝埌鍟嗗搧椤甸潰
	
		return "index";
		}
		return "login";
	}
	
	
	@RequestMapping("/userlogin")
	public String userlogin(HttpSession session, String userId,Integer userHousenum, Model model) throws Exception{
		//璋冪敤service杩涜鐢ㄦ埛韬唤楠岃瘉
		Houseuser houseuser = HouseuserService.findUserByuserIdanduserHousenum(userId, userHousenum);
		if(houseuser != null){
		//鍦╯ession涓繚瀛樼敤鎴疯韩浠戒俊鎭�
		session.removeAttribute("adminId");
		session.setAttribute("userId", houseuser.getUserId());
		session.setAttribute("userHousenum", houseuser.getUserHousenum());
		//閲嶅畾鍚戝埌鍟嗗搧椤甸潰
		model.addAttribute("houseuser", houseuser);
	
		return "choose";
		}
		return "ulogin";
	}
	//閫�鍑�
	@RequestMapping("/adminlogout")
	public String adminlogout(HttpSession session) throws Exception{
		//娓呯┖session
		session.removeAttribute("adminId");
		//閲嶅畾鍚戝埌鐧诲叆椤甸潰
		return "login";
	}
	
	@RequestMapping("/userlogout")
	public String userlogout(HttpSession session) throws Exception{
		//娓呯┖session
		session.removeAttribute("userId");
		//閲嶅畾鍚戝埌鐧诲叆椤甸潰
		return "ulogin";
	}
}
